﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public  class adminsDal
    {
        /// <summary>
        /// 查信息
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static ms_admins Querybymerid(string merchantid)
        {
            string sql = "select * from ms_admins where merchant_id=@merchant_id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            ms_admins admins = new ms_admins();
            if (rd.Read())
            {
                admins.id = rd["id"].ToString();
                admins.username = rd["username"].ToString();
                admins.password = rd["password"].ToString();
                admins.merchant_id = rd["merchant_id"].ToString();
                admins.nickname = rd["nickname"].ToString();
                admins.is_master = rd["is_master"].ToString();
                admins.login_ip = rd["login_ip"].ToString();
                admins.login_time = rd["login_time"].ToString();
                admins.register_time = rd["register_time"].ToString();
                admins.update_time = rd["update_time"].ToString();
                admins.status = rd["status"].ToString();
            }
            rd.Close();
            return admins;

        }
        /// <summary>
        /// 创建账户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <param name="merchantid"></param>
        /// <param name="nickname"></param>
        /// <returns></returns>

        public static int Addnewadmin(string name, string pwd,string merchantid,string nickname,string admintype)
        {
            string sql = "insert into ms_admins(username,password,merchant_id,nickname,admintype,is_master,register_time) values(@username,@password,@merchant_id,@nickname,@admintype,@is_master,@register_time)";
            MySqlParameter[] values = new MySqlParameter[7];
            values[0] = new MySqlParameter("username", name);
            values[1] = new MySqlParameter("password", pwd);
            values[2] = new MySqlParameter("merchant_id", merchantid);
            values[3] = new MySqlParameter("nickname", nickname);
            values[4] = new MySqlParameter("admintype", admintype);
            values[5] = new MySqlParameter("is_master", "0");
            values[6] = new MySqlParameter("register_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return DBHelper.ExcutUpdate(sql, values);

        }
        /// <summary>
        /// 判定该账号是否增加
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Querybyphone(string phone,string merchantid)
        {
            string sql = "select count(*) as count from ms_admins where username=@username and merchant_id=@merchant_id and status=1";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("username", phone);
            values[1] = new MySqlParameter("merchant_id", merchantid);
            int count = 0;
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            if (rd.Read())
            {
                count = Convert.ToInt32(rd["count"]);
            }
            rd.Close();
            return count;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Updatepwd(string pwd, string adminid)
        {
            string sql = "update ms_admins set password=@password where id=@id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("password", pwd);
            values[1] = new MySqlParameter("id", adminid);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 查主账号下子账号
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static List<ms_admins> Querybymerchantid(string merchantid)
        {
            string sql = "select * from ms_admins where merchant_id=@merchant_id and is_master=0";
            List<ms_admins> admlist = new List<ms_admins>();
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("merchant_id", merchantid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql,values);
            while (rd.Read())
            {
                ms_admins admins = new ms_admins();
                admins.id = rd["id"].ToString();
                admins.username = rd["username"].ToString();
                admins.password = rd["password"].ToString();
                admins.merchant_id = rd["merchant_id"].ToString();
                admins.nickname = rd["nickname"].ToString();
                admins.is_master = rd["is_master"].ToString();
                admins.login_ip = rd["login_ip"].ToString();
                admins.login_time = rd["login_time"].ToString();
                admins.register_time = rd["register_time"].ToString();
                admins.update_time = rd["update_time"].ToString();
                admins.status = rd["status"].ToString();
                admins.admintype = rd["admintype"].ToString();
                admlist.Add(admins);
            }
            rd.Close();
            return admlist;
        }
        /// <summary>
        /// 修改账号状态1启用0停用
        /// </summary>
        /// <param name="username"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static int Deladmin(string username)
        {
            string sql = "delete from ms_admins where username=@username";
            MySqlParameter[] values = new MySqlParameter[1];          
            values[0] = new MySqlParameter("username", username);
            return DBHelper.ExcutUpdate(sql, values);
        }
    }
}
